package mapper;

import com.sqsf.javabean.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserMapper {
    List<User> selectAll();
    //之前在进行多个参数传递时 只能使用mybatis提供的参数名称
    //可以使用对应 注解 @Param进行自定义参数绑定
    User selectByUsernameAndPassword(@Param("username") String username,@Param("password") String password);
    //mybatis注解可以简化mapper的配置 甚至可以不书写mapper

    @Select("select * from user where uid=#{uid}")
    User selectByUid(int uid);

    @Insert("insert into user (username,password,phone)values(#{username},#{password},#{phone})")
    int insertUser(User user);

    @Delete("delete from user where uid=#{uid}")
    int deleteByUid(int uid);

    @Update("update user set password=#{password} where uid=#{uid}")
    int updatePasswordByUid(@Param("uid") int uid,@Param("password") String password);

    @Select("select uid,username uname,password pwd,phone tel from user")
    @Results(id = "u",value = {
            @Result(column = "uname",property = "username"),
            @Result(column = "pwd",property = "password"),
            @Result(column = "tel",property = "phone")
    })
    List<User> selectAllUser();


    @Select("select uid,username uname,password pwd,phone tel from user")
    @ResultMap("u")
    List<User> selectAllUser1();

    @Select("select u.*,r.* from user u left join user_role ur on u.uid=ur.uid left join role r on ur.rid=r.rid")
    @Results(id = "u_r",value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(column = "rid",property = "r.rid"),
            @Result(column = "name",property = "r.name"),
            @Result(column = "desc",property = "r.desc")
    })
    List<User> selectUser_Role();

    @Select("select * from user")
    @Results(id = "u_r1",value = {
            @Result(id = true,column = "uid",property = "uid"),
            @Result(property = "r",column = "uid",one=@One(
                    select = "mapper.RoleMapper.selectByUid",
                    fetchType = FetchType.LAZY
            ))
    })
    List<User> selectUser_Role1();

    @Select({"<script>   select * from user\n" +
            "        <where>\n" +
            "            <if test=\"uid!=0\">and uid=#{uid}</if>\n" +
            "            <if test=\"username!=null\">and username=#{username}</if>\n" +
            "            <if test=\"password!=null\">and password=#{password}</if>\n" +
            "            <if test=\"phone!=null\">and phone=#{phone}</if>\n" +
            "        </where></script>"})
    List<User> selectByUser(User user);

}

